package com.ydkj.a37e_mall.Classes.Acitivity.BaseActivity;

import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.ydkj.a37e_mall.Classes.Applicatiom.MyApplication;
import com.ydkj.a37e_mall.R;
import com.ydkj.a37e_mall.Resource.Utils.ImageTools;
import com.ydkj.a37e_mall.Resource.Utils.SharedPreferencesUtil;
import com.ydkj.a37e_mall.Resource.View.StatusBarUtil;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.wechat.favorite.WechatFavorite;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by Administrator on 2017/1/11.
 * 分销推荐
 */

public class RecommendActivity extends BaseActivity implements View.OnClickListener {
    private Activity mActivity;
    private SharedPreferencesUtil sharedPreferencesUtil;
    private MyApplication myApplication;
    private ImageView img_back, img_erweima;
    private RelativeLayout rly_save_erweima;
    private LinearLayout lly_qq, lly_weixin, lly_copy_link, lly_weibo, lly_weixin_friend;
    private Bitmap bitmap;
    private TextView tv_copy_recommendationcode, tv_recommendationcode;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recommendlayout);
        mActivity = this;
        init();
//        initWindow();
    }

    private void initWindow() {
        StatusBarUtil.setColor(mActivity, getResources().getColor(R.color.A000000), 0);

    }

    private void init() {
        sharedPreferencesUtil = SharedPreferencesUtil.getInstance(mActivity);
        myApplication = MyApplication.getInstance();
        img_back = (ImageView) mActivity.findViewById(R.id.img_back);
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("sssss", "sdsdsd");
                finish();
            }
        });

        rly_save_erweima = (RelativeLayout) mActivity.findViewById(R.id.rly_save_erweima);
        tv_copy_recommendationcode = (TextView) mActivity.findViewById(R.id.tv_copy_recommendationcode);
        tv_recommendationcode = (TextView) mActivity.findViewById(R.id.tv_recommendationcode);
        lly_qq = (LinearLayout) mActivity.findViewById(R.id.lly_qq);
        lly_weixin = (LinearLayout) mActivity.findViewById(R.id.lly_weixin);
        lly_copy_link = (LinearLayout) mActivity.findViewById(R.id.lly_copy_link);
        lly_weibo = (LinearLayout) mActivity.findViewById(R.id.lly_weibo);
        lly_weixin_friend = (LinearLayout) mActivity.findViewById(R.id.lly_weixin_friend);
        img_erweima = (ImageView) mActivity.findViewById(R.id.img_erweima);


        ImageLoader.getInstance().displayImage(myApplication.getDataEntity().getQrcode(), img_erweima, displayImageOptions);

        new Task().execute(myApplication.getDataEntity().getQrcode());

        tv_recommendationcode.setText(myApplication.getDataEntity().getUuid());


        rly_save_erweima.setOnClickListener(this);
        tv_copy_recommendationcode.setOnClickListener(this);
        lly_qq.setOnClickListener(this);
        lly_weixin.setOnClickListener(this);
        lly_copy_link.setOnClickListener(this);
        lly_weibo.setOnClickListener(this);
        lly_weixin_friend.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rly_save_erweima:

                ImageTools.saveImageToGallery(mActivity, bitmap);//保存图片

//                MediaScannerConnection.scanFile(mActivity, new String[]{Environment.getExternalStorageDirectory() + "/37mallphoto"}, null, null);
                break;
            case R.id.tv_copy_recommendationcode: //复制推荐码
                ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                Log.e("ssss", "text==>" + tv_recommendationcode.getText());
                cm.setText(tv_recommendationcode.getText());
                Toast.makeText(mActivity, "复制推荐码成功", Toast.LENGTH_SHORT).show();
                break;
            case R.id.lly_qq:
                Platform plat = ShareSDK.getPlatform(QQ.NAME);
                showShare(plat.getName(), myApplication.getDataEntity().getQrcode());
                break;
            case R.id.lly_weixin:
                Platform plat1 = ShareSDK.getPlatform(Wechat.NAME);
                showShare(plat1.getName(), myApplication.getDataEntity().getQrcode());
                break;
            case R.id.lly_copy_link:
                Platform plat2 = ShareSDK.getPlatform(WechatFavorite.NAME);
                showShare(plat2.getName(), myApplication.getDataEntity().getQrcode());
                break;
            case R.id.lly_weibo:
                Platform plat3 = ShareSDK.getPlatform(SinaWeibo.NAME);
                showShare(plat3.getName(), myApplication.getDataEntity().getQrcode());
                break;
            case R.id.lly_weixin_friend:
                Platform plat4 = ShareSDK.getPlatform(WechatMoments.NAME);
                showShare(plat4.getName(), myApplication.getDataEntity().getQrcode());
                break;
        }
    }

    /**
     * new Task().execute(urlPath);
     * 从服务端获取二维码的地址调用这句 再生成bitmap
     * <p>
     * 异步线程下载图片
     */
    class Task extends AsyncTask<String, Integer, Void> {
        protected Void doInBackground(String... params) {
            bitmap = ImageTools.GetImageInputStream((String) params[0]);
            return null;
        }

    }


    private void showShare(String platform, String url) {
        final OnekeyShare oks = new OnekeyShare();
        //指定分享的平台，如果为空，还是会调用九宫格的平台列表界面
        if (platform != null) {
            oks.setPlatform(platform);
        }
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
//        oks.setTitle("二维码分享");
        // titleUrl是标题的网络链接，仅在Linked-in,QQ和QQ空间使用
//        oks.setTitleUrl(url);
        // text是分享文本，所有平台都需要这个字段
//        oks.setText("二维码分享");
        //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
        oks.setImageUrl(url);
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
//        oks.setUrl(url);
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
//        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
//        oks.setSite("ShareSDK");
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl(url);

        //启动分享
        oks.show(this);
    }
}
